package com.or.cloud.view.activity;

import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;

import com.blankj.utilcode.util.ToastUtils;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.or.cloud.R;
import com.or.cloud.databinding.ActivityMainBinding;
import com.or.cloud.view.GlideEngine;
import com.or.common.base.CMBaseActivity;
import com.or.common.base.CMBaseViewModel;
import com.or.common.utils.CMPermissionUtils;

import java.util.ArrayList;

public class MainActivity extends CMBaseActivity<ActivityMainBinding, CMBaseViewModel> {

    ActivityResultLauncher<String[]> mResultMedia;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        binding.ctbTitle.setBackGone().setCenterTitle("橘");
        mResultMedia = CMPermissionUtils.registerForActivityResultMedia(this, new CMPermissionUtils.OnPermissionsCallback() {
            @Override
            public void onPermissionsGranted() {
                // 权限被用户授予，可以继续执行后续逻辑
                ToastUtils.showShort("申请权限成功");
            }

            @Override
            public void onPermissionsDenied() {
                // 权限被用户拒绝
                ToastUtils.showShort("权限被拒绝");
            }
        });
    }

    @Override
    protected void initListener() {
        //视频剪辑
        binding.tvClip.setOnClickListener(view -> {
            onOption("clip");
        });
        //视频合并
        binding.tvHb.setOnClickListener(view -> {
            onOption("hb");
        });
    }

    private void selectLocalMedia(String option) {
        PictureSelector.create(MainActivity.this)
                .openGallery(SelectMimeType.ofVideo())
                .setMaxSelectNum(1)
                .setImageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        switch (option) {
                            case "clip":
                                MediaClipActivity.start(MainActivity.this, result);
                                break;
                            case "hb":
//                                MediaClipActivity.start(MainActivity.this);
                                break;
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    private void onOption(String option) {
        if (CMPermissionUtils.hasReadExternalStoragePermission(this)) {
            selectLocalMedia(option);
        } else {
            mResultMedia.launch(CMPermissionUtils.getReadExternalStoragePermissions());
        }
    }

}
